using System;
using Gtk;

namespace twitalotgtk
{
	class MainClass
	{
		private static string _appConfigPath = string.Empty;
		private static Settings _settings = new Settings();		
		const string APP_NAME = "twitalot";
		
		private static MainWindow _win = null;
		
		public static Settings Settings
		{
			get{ return _settings; }
			set{ _settings = value;}
		}
		
		public static string AppConfigPath
		{
			get { return _appConfigPath; }
		}
		
		public static void Main (string[] args)
		{
			Application.Init ();
			
			_appConfigPath = string.Format("{0}/{1}", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), APP_NAME);
			
			if(!System.IO.Directory.Exists(_appConfigPath))
			{
				System.IO.Directory.CreateDirectory(_appConfigPath);
			}		
			
			string settingsPath = string.Format("{0}/{1}", _appConfigPath, "settings.xml");
			_settings = Settings.Load(settingsPath);
					
			_win = new MainWindow ();
			_win.DeleteEvent += delegate {
				_settings.Save(settingsPath);
			};
			
			StatusIcon icon = new StatusIcon(_win.Icon);
			icon.Visible = true;
			icon.Activate += delegate { _win.Visible = !_win.Visible; };
			icon.Tooltip = "Twitalot";
			
			icon.PopupMenu += HandlePopupMenu;
			
			_win.ShowAll ();			
			
			Application.Run ();
		}

		static void HandlePopupMenu(object o, PopupMenuArgs args)
		{
			Menu popupMenu = new Menu();
			CheckMenuItem menuItemShow = new CheckMenuItem("_Show Twitalot");
		 	menuItemShow.Active = _win.Visible;
			menuItemShow.Activated += delegate {_win.Visible = !_win.Visible;};
			popupMenu.Add(menuItemShow);
			
			SeparatorMenuItem sep = new SeparatorMenuItem();
			popupMenu.Add(sep);
			
			ImageMenuItem menuItemQuit = new ImageMenuItem ("_Quit");
			Gtk.Image appimg = new Gtk.Image(Stock.Quit, IconSize.Menu);
			menuItemQuit.Image = appimg;
			popupMenu.Add(menuItemQuit);
			// Quit the application when quit has been clicked.
			menuItemQuit.Activated += delegate { Application.Quit(); };
			popupMenu.ShowAll();
			popupMenu.Popup();
		}
	}
}